"use strict";
cc._RF.push(module, '2bbb81/lJ5O/a6F+a4Y7/ZP', 'enemy');
// script/enemy.js

"use strict";

cc.Class({
  "extends": cc.Component,
  properties: {},
  onLoad: function onLoad() {
    // 设置当前敌人的血量
    this.hp = 5;
    this.sp = cc.v2(0.0); // 敌人的移动速度

    this._speed = 80;
    this.anima = ''; // 拿到当前动画状态

    this.enAni = this.node.getChildByName('body').getComponent(cc.Animation);
    this.anima = '';
    console.log('来了', this.node);
    this.enAni.on("finished", this.onAnimEnemyFinshed, this); // 控制当前人物是像什么方向移动

    this.moveRight = false;
    this.moveLeft = true;
    this.playerNode = cc.find('Canvas/bg/hero');
    console.log('this.playerNode', this.playerNode);
    cc.systemEvent.on("keydown", this.enemyOnKeydown, this);
  },
  // 监听键盘按下事件
  enemyOnKeydown: function enemyOnKeydown(e) {
    // cc.log(e)
    if (e.keyCode == 81) {
      this.characterControl();
    }
  },
  // 删除挂载
  onDestroy: function onDestroy() {
    cc.systemEvent.off("keydown", this.enemyOnKeydown, this); // 被销毁了就没有node节点了

    console.log('删除'); // this.enAni.off("finished",this.onAnimEnemyFinshed,this)
  },
  // 监听动画结束
  onAnimEnemyFinshed: function onAnimEnemyFinshed(e, data) {
    var _this = this;

    console.log("动画结束", this.node);
    this.anima = '';

    if (this.hp <= 0) {
      setTimeout(function () {
        _this.node.destroy();
      }, 50); // this.node.destroy()
    }
  },
  // 播放动画
  setAni: function setAni(anima) {
    if (this.anima == anima || anima == '') return; // 选择播放那段动画

    this.anima = anima;
    this.enAni.play(anima);
  },
  // 子组件回调
  hurt: function hurt() {
    this.hp--;
    this.setAni("hurt");
  },
  // 碰撞回调
  // onCollisionEnter(other,self){
  //     console.log("other,self",other.node._name);
  //     if(other.node._name === 'hero'){
  //         this.hp--
  //         this.enAni.play("hurt")
  //         if(this.hp == 0){
  //             this.node.destroy()
  //         }
  //     }
  // },
  update: function update(dt) {
    this.characterControl();
  },
  // 控制当前的移动方法
  changeDirection: function changeDirection(type) {
    if (type > 0) {
      this.moveLeft = false;
      this.moveRight = true;
    } else {
      this.moveLeft = true;
      this.moveRight = false;
    }
  },
  // 控制怪的行为
  characterControl: function characterControl() {
    // 第一步 先判断当前的人物在他的左面还是右面
    var p_position = this.playerNode.position;
    var e_position = this.node.position;
    var distance = cc.Vec2.distance(p_position, e_position); // x相隔距离

    var v = p_position.sub(e_position);
    cc.log('distance', distance);
    cc.log('current distance is ', v.x); // 控制方向

    this.changeDirection(v.x); // 行为控制

    this.behaviorControl(distance);
  },
  // 行为控制
  behaviorControl: function behaviorControl(distance) {
    if (distance < 25 && distance > -25) {
      cc.log("攻击"); // 如果要攻击第一步先停止走路
      // 停止现在正在播放的动画
      // 停止移动

      this.moveLeft = false;
      this.moveRight = false;
      this.lv.x = 0;
      var anima = this.anima;
      anima = 'accack';

      if (anima) {
        this.setAni(anima);
      }
    } else if (distance < 150 && distance > -150) {
      cc.log("追击");
      this.move();
    } else {
      cc.log("如果离得太远就进行巡逻");
    }
  },
  // 控制敌人的移动
  move: function move() {
    // // 改变当前的面朝方向
    var scaleX = Math.abs(this.node.scaleX); // 获取当前动画

    var anima = this.anima; // 拿到一个当前的向前速度

    this.lv = this.node.getComponent(cc.RigidBody).linearVelocity;

    if (this.moveLeft) {
      // console.log("左一");
      // this.sp.y = -5;
      this.sp.x = -1;
      this.node.scaleX = -scaleX;
      anima = 'run';
    } else if (this.moveRight) {
      // this.sp.y = 5;
      this.sp.x = 1;
      this.node.scaleX = scaleX;
      anima = 'run';
    } else {
      // this.sp.y = 0
      this.sp.x = 0;
      anima = '';
    } // 如果有当前速度的话 那么就可以移动  现在只有左右 所以不用y轴


    if (this.sp.x) {
      // this.lv.y = this.sp.y * this._speed
      this.lv.x = this.sp.x * this._speed;
    } else {
      // this.lv.y = 0
      this.lv.x = 0;
    } // this.lv.x = this.sp.x * this._speed


    if (anima) {
      this.setAni(anima);
    }

    this.node.getComponent(cc.RigidBody).linearVelocity = this.lv;
  },
  start: function start() {} // update (dt) {},

});

cc._RF.pop();